package com.fh.service.impl;

import com.fh.mapper.UserMapper;
import com.fh.model.DataTablePageBean;
import com.fh.model.DataTableResult;
import com.fh.model.User;
import com.fh.model.UserSearch;
import com.fh.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhaolh
 * @title: UserServiceImpl
 * @projectName ssm-demo
 * @description: TODO
 * @date 2021/11/2 11:18
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public DataTableResult queryUserList(DataTablePageBean pageBean, UserSearch search) {
        if (StringUtils.isNotBlank(search.getHobby())){
            search.setHobbies(search.getHobby().split(","));
        }

        // 查询总条数
        Integer count = userMapper.queryUserCount(search);

        List<User> userList = userMapper.queryUserList(search);

        DataTableResult dataTableResult = new DataTableResult(pageBean.getDraw(),count,count,userList);
        return dataTableResult;
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public void deleteBatch(List list) {
        userMapper.deleteBatch(list);
    }
}
